<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="db.ReceiveorderDAO"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.ProjectDAO"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.ParseException"%>
<%@page import="db.RetmatDAO"%>
<%@page import="db.OutpurmatDAO"%>
<%@page import="db.BormatDAO"%>
<%@ include file="/jsp/common/utility/logined.jsp"%>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ReceiveorderDAO receiveorderDao = (ReceiveorderDAO)ctx.getBean("ReceiveorderDAO");
	ReceivepurmatDAO receivepurmatDao = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	RetmatDAO retmatDao = (RetmatDAO)ctx.getBean("RetmatDAO");
	OutpurmatDAO outpurmatDao = (OutpurmatDAO)ctx.getBean("OutpurmatDAO");
	BormatDAO bormatDao = (BormatDAO)ctx.getBean("BormatDAO");
	ProjectDAO projectDao = (ProjectDAO)ctx.getBean("ProjectDAO");
	UserDAO userDao = (UserDAO)ctx.getBean("UserDAO");
	
	String beginDate = request.getParameter("beginDate");
	String endDate = request.getParameter("endDate");
	
	Integer uid = (Integer)session.getAttribute("id");
	Integer teamId = (Integer)session.getAttribute("tid");
	
	if((beginDate != null && !"".equals(beginDate)) && (endDate != null && !"".equals(endDate))){
		getZxBalance(receivepurmatDao,session,request,beginDate,endDate);
		getTyBalance(receivepurmatDao,session,request,beginDate,endDate);
		getZxReceive(receivepurmatDao,session,request,beginDate,endDate);
		getTyReceive(receivepurmatDao,session,request,beginDate,endDate);
		getZxReturn(retmatDao,session,request,beginDate,endDate);
		getTyReturn(retmatDao,session,request,beginDate,endDate);
		getZxOther(receivepurmatDao,session,request,beginDate,endDate);
		getTyOther(receivepurmatDao,session,request,beginDate,endDate);
		getZxOut(outpurmatDao,session,request,beginDate,endDate);
		getTyOut(outpurmatDao,session,request,beginDate,endDate);
		getZxBorrow(bormatDao,session,request,beginDate,endDate);
		getTyBorrow(bormatDao,session,request,beginDate,endDate);
	}
	
	User user = userDao.findById(uid);
	request.setAttribute("user",user);
	
	request.setAttribute("beginDate",beginDate);
	request.setAttribute("endDate",endDate);
	
	pageContext.forward("/vm/statistics/report_1.vm");
%>

<%!
	public void getZxBalance(ReceivepurmatDAO receivepurmatDao , HttpSession session, HttpServletRequest request, String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "isDelete", 0});
		where.add(new Object[]{"eq", "teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "date", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "date", dateVal});
		}
		
		where.add(new Object[]{"eq", "subStore", 1});
		
		request.setAttribute("zxBalance",receivepurmatDao.getSum(where ,new String[]{"receiveorder","substore"} ,"purNum"));
	}

	public void getTyBalance(ReceivepurmatDAO receivepurmatDao ,HttpSession session, HttpServletRequest request,String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "isDelete", 0});
		where.add(new Object[]{"eq", "teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "date", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "date", dateVal});
		}
		
		where.add(new Object[]{"eq", "subStore", 2});
		
		request.setAttribute("tyBalance",receivepurmatDao.getSum(where ,new String[]{"receiveorder","substore"} ,"purNum"));
	}
	
	public void getZxReceive(ReceivepurmatDAO receivepurmatDao , HttpSession session, HttpServletRequest request, String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "isDelete", 0});
		where.add(new Object[]{"eq", "teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "date", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "date", dateVal});
		}
		
		where.add(new Object[]{"eq", "subStore", 1});
		where.add(new Object[]{"ne", "r.code", ""});
		
		request.setAttribute("zxReceive",receivepurmatDao.getSum(where ,new String[]{"receiveorder","substore"} ,"importNum"));
	}
	
	public void getTyReceive(ReceivepurmatDAO receivepurmatDao , HttpSession session, HttpServletRequest request, String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "isDelete", 0});
		where.add(new Object[]{"eq", "teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "date", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "date", dateVal});
		}
		
		where.add(new Object[]{"eq", "subStore", 2});
		where.add(new Object[]{"ne", "r.code", ""});
		
		request.setAttribute("tyReceive",receivepurmatDao.getSum(where ,new String[]{"receiveorder","substore"} ,"importNum"));
	}
	
	public void getZxReturn(RetmatDAO retmatDao,HttpSession session, HttpServletRequest request,String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "or.isDelete", 0});
		where.add(new Object[]{"eq", "r.teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "r.okDate", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "r.okDate", dateVal});
		}
		
		where.add(new Object[]{"eq", "or.subStore", 1});
		
		request.setAttribute("zxReturn",retmatDao.getSum(where ,new String[]{"retreq","outpurmat","receivepurmat","substore"} ,"okNum"));
	}
	
	public void getTyReturn(RetmatDAO retmatDao,HttpSession session, HttpServletRequest request,String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "or.isDelete", 0});
		where.add(new Object[]{"eq", "r.teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "r.okDate", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "r.okDate", dateVal});
		}
		
		where.add(new Object[]{"eq", "or.subStore", 2});
		
		request.setAttribute("tyReturn",retmatDao.getSum(where ,new String[]{"retreq","outpurmat","receivepurmat","substore"} ,"okNum"));
	}
	
	public void getZxOther(ReceivepurmatDAO receivepurmatDao , HttpSession session, HttpServletRequest request, String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "isDelete", 0});
		where.add(new Object[]{"eq", "teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "date", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "date", dateVal});
		}
		
		where.add(new Object[]{"eq", "subStore", 1});
		where.add(new Object[]{"eq", "r.code", ""});
		
		request.setAttribute("zxOther",receivepurmatDao.getSum(where ,new String[]{"receiveorder","substore"} ,"importNum"));
	}
	
	public void getTyOther(ReceivepurmatDAO receivepurmatDao , HttpSession session, HttpServletRequest request, String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "isDelete", 0});
		where.add(new Object[]{"eq", "teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "date", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "date", dateVal});
		}
		
		where.add(new Object[]{"eq", "subStore", 2});
		where.add(new Object[]{"eq", "r.code", ""});
		
		request.setAttribute("tyOther",receivepurmatDao.getSum(where ,new String[]{"receiveorder","substore"} ,"importNum"));
	}
	
	public void getZxOut(OutpurmatDAO outpurmatDao , HttpSession session, HttpServletRequest request, String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "r.isDelete", 0});
		where.add(new Object[]{"eq", "o.teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "o.outReqDate", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "o.outReqDate", dateVal});
		}
		
		where.add(new Object[]{"eq", "r.subStore", 1});
		
		request.setAttribute("zxOut",outpurmatDao.getSum(where ,new String[]{"outreq","receivepurmat","substore"} ,"outReqNum"));
	}
	
	public void getTyOut(OutpurmatDAO outpurmatDao , HttpSession session, HttpServletRequest request, String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "r.isDelete", 0});
		where.add(new Object[]{"eq", "o.teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "o.outReqDate", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "o.outReqDate", dateVal});
		}
		
		where.add(new Object[]{"eq", "r.subStore", 2});
		
		request.setAttribute("tyOut",outpurmatDao.getSum(where ,new String[]{"outreq","receivepurmat","substore"} ,"outReqNum"));
	}
	
	public void getZxBorrow(BormatDAO bormatDao , HttpSession session, HttpServletRequest request, String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "r.isDelete", 0});
		where.add(new Object[]{"eq", "b.teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "b.okDate", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "b.okDate", dateVal});
		}
		
		where.add(new Object[]{"eq", "r.subStore", 1});
		
		request.setAttribute("zxBorrow",bormatDao.getSum(where ,new String[]{"borreq","receivepurmat","substore"},"okNum"));
	}
	
	public void getTyBorrow(BormatDAO bormatDao , HttpSession session, HttpServletRequest request, String beginDate, String endDate) throws ParseException{
		Integer uid = (Integer)session.getAttribute("id");
		Integer teamId = (Integer)session.getAttribute("tid");
		
		List where = new ArrayList();
		
		where.add(new Object[]{"eq", "r.isDelete", 0});
		where.add(new Object[]{"eq", "b.teamId", teamId});
		
		if(beginDate != null && !"".equals(beginDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			where.add(new Object[]{"ge", "b.okDate", dateVal});
		}
		
		if(endDate != null && !"".equals(endDate)){
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
			where.add(new Object[]{"le", "b.okDate", dateVal});
		}
		
		where.add(new Object[]{"eq", "r.subStore", 2});
		
		request.setAttribute("tyBorrow",bormatDao.getSum(where ,new String[]{"borreq","receivepurmat","substore"},"okNum"));
	}
%>